# author: Jochen Rehmert, University of Basel
# journal: Journal of Politics
# article: Intra-party competition, geographic responsiveness 
#           and incumbent deselection in closed-list PR
# date: 12th August 2024
# content: Script to replicate Table 5 in the Appendix
# needs data: "nomination.RDS"
# see also: "data_description_table5.txt"

# directory
setwd("")

sink("log_rep_table5.txt")
cat("start logging\n")
dat <- readRDS("nomination.rds")


# --- unchallenged --- #
summary(m1 <- glm(no_comp ~ as.factor(land) + as.factor(rounds) +
                    inc + rank_electability + female_rank, data = dat, family = binomial("logit")))

# --- rounds of voting --- #
summary(m2 <- glm(rounds ~ as.factor(land) + as.factor(election)  +
                    inc + rank_electability + female_rank, data = dat, family = quasipoisson))

# --- margin --- #
summary(m3 <- lm(margin ~ as.factor(land) + as.factor(election) + as.factor(rounds) +
                    inc + rank_electability + female_rank, data = dat))

# --- number of candidates --- #
summary(m4 <- glm(num_cand ~ as.factor(land) + as.factor(election)  +
                    inc + rank_electability + female_rank, data = dat, family = quasipoisson))

# --- eff. no. of candidates --- #
summary(m5 <- lm(enec ~ as.factor(land) + as.factor(election) + as.factor(rounds) +
                   inc + rank_electability + female_rank, data = dat))

library(stargazer)
#################################################
# Table 5, Appendix
#################################################
cat("create table 5\n")
stargazer(list(m1, m3, m4, m5), 
          omit = c("land","election","rounds"),
          covariate.labels = c("Incumbent Competes",
                               "Rank's Pr(Election)",
                               "Women's Rank"),
          add.lines = list(c("Bundesland Fixed-Effects", rep("Yes",4) ),
                           c("Year Fixed-Effects", "No", rep("Yes", 3)),
                           c("Round of Voting Fixed-Effects", rep("Yes",4))))

cat("end logging")
sink()
